from django.shortcuts import render, redirect
from app01.models import User
from app01.utils.form import UserLoginModelForm

# Create your views here.


def login(request):
    coo = request.session.get('info', "")
    form = UserLoginModelForm()
    if request.method == 'GET':
        return render(request, "login/login.html", {"form": form, "coo": coo})
    form = UserLoginModelForm(data=request.POST)
    if form.is_valid():
        admin_object = User.objects.filter(**form.cleaned_data).first()
        request.session["info"] = {'id': admin_object.id, 'email': admin_object.email, 'name': admin_object.name}
        request.session.set_expiry(60 * 60 * 24 * 7)
        return redirect("/index/")
    else:
        return render(request, "login/login.html", {"form": form, "coo": coo})